﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using System.IO;
using MISAPP.Classify.DataAccess;
using MISAPP.MISInterface;

namespace MISAPP.Classify.Service
{
    public class Service_Matrix
    {
        protected string dataService = "";
        protected string dllName = "DataAccess.dll";
        protected object da = null;

        protected object DA
        {
            get
            {
                if (da == null)
                {
                    da = getDataObject();
                }
                return da;
            }
        }

        protected string team = string.Empty;

        protected object getDataObject()
        {
            object o = null;
            Type t = null;
            string dll,baseDir;
            baseDir = AppDomain.CurrentDomain.BaseDirectory;
            if (File.Exists(baseDir + dllName))
            {
                dll = string.Format("{0}{1}", baseDir, dllName);
            }
            else
            {
                dll = string.Format("{0}bin\\{1}", baseDir, dllName);
            }
            System.Reflection.Assembly instance = System.Reflection.Assembly.LoadFrom(dll);
            t = instance.GetType(dataService);
            o = System.Activator.CreateInstance(t);
            return o;
        }

        protected List<Squared> GetParameterValue(object p_da, object[] p_level)
        {            
            Type t = p_da.GetType();
            System.Reflection.MethodInfo mi = t.GetMethod("GetSquaredAreaByLevel"
                , new Type[] { typeof(System.String), typeof(System.String) });

            if (mi == null)
            { throw new Exception("DA NOT Found GetSquaredAreaByLevel"); }
             DataTable dt = mi.Invoke(p_da, p_level) as DataTable ;            
            List<Squared> seqs = new List<Squared>();
            foreach (DataRow row in dt.Rows)
            {
                seqs.Add(new Squared(row["fldname"].ToString()
                , row["fldvalue"].ToString().Split(new char[] { ',' })));
            }
            return seqs;
        }

    }



}
